<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Idc extends Controller_Admin_Based
{

    public function action_list()
    {

        $pagination_arr = Paginationhelp::get_data('idc', 'id_idc');

        $games = Game::instances()->get_game();

        $data = array(
            'result' => $pagination_arr['data'],
            'games' => $games,
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/idc_list', $data);
        $this->response->body($view);
    }

    public function action_add()
    {
        $games = Game::instances()->get_game();
        $types = Type::instances()->get_type();
        $data = array(
            'games' => $games,
            'types'=>$types
        );
        
        $view = Viewer::display('admin/idc_add', $data);
        $this->response->body($view);
    }

    public function action_do_add()
    {

        if ($_POST)
        {
            if ($_POST['addtime'] == '')
            {
                $_POST['addtime'] = Time::instance()->get_mk_time(date('Y-m-d',time()));
            }
            else
            {
                $_POST['addtime'] = Time::instance()->get_mk_time($_POST['addtime']);
            }
            $_POST['updatetime'] = time();


            $idc = Idc::instances()->set($_POST);
            if ($idc)
            {
                Message::set('服务器添加成功！');
                Request::current()->redirect('/admin/idc/add');
            }
        }
    }

    public function action_edit()
    {
        $id_idc = $this->request->param('id');

        $idc = Idc::instances($id_idc)->get();

        $games = Game::instances()->get_game();
         $types = Type::instances()->get_type();
        $view = Viewer::display('admin/idc_edit', array('data' => $idc, 'games' => $games,'types'=>$types));
        $this->response->body($view);
    }

    public function action_do_edit()
    {
        $id_idc = $this->request->param('id');
        $idc = Idc::instances($id_idc)->get();
        if ($_POST)
        {
            $_POST['updatetime'] = time();
            if ($_POST['addtime'] == '')
            {
                $_POST['addtime'] = Time::instance()->get_mk_time(date('Y-m-d',time()));
            }
            else
            {
         
               
                
                $_POST['addtime'] = Time::instance()->get_mk_time($_POST['addtime']);
            }

            $idc = Idc::instances($id_idc)->update($_POST);
            if ($idc)
            {
                Message::set('服务器编辑成功！');
                Request::current()->redirect('/admin/idc/edit/' . $id_idc);
            }
        }
        $idc = Idc::instances($id_idc)->get();
     $types = Type::instances()->get_type();
        $view = Viewer::display('admin/idc_edit', array('data' => $idc, 'games' => $games,'types'=>$types));
        $this->response->body($view);
    }

    public function action_delete()
    {
        $id_idc = $this->request->param('id');
        $flag = Idc::instances($id_idc)->delete();
        if ($flag)
        {
            Message::set('服务器成功');
            Request::current()->redirect('/admin/idc/list');
        }
        else
        {
            Message::set('服务器删除失败', 'error');
            Request::current()->redirect('/admin/idc/list');
        }
    }

}